#include "ActionDessinGrab.h"
#include "Dessin.h"


ActionDessinGrab::ActionDessinGrab() : ActionDessin(){}

void ActionDessinGrab::OnMousePressed(Dessin* d,wxMouseEvent& event){

    selected =true;
    d->SetCursor(wxCursor(wxCURSOR_HAND));
    debut_mouv_x=event.m_x;
    debut_mouv_y=event.m_y;
}

void ActionDessinGrab::OnRelease(Dessin* d,wxMouseEvent& event){
    selected=false;
    d->SetCursor(wxCursor(wxCURSOR_ARROW));
}

void ActionDessinGrab::OnMotion(Dessin* d,wxMouseEvent& event){
    if(selected){
        ActionDessinGrab::deplacer(d,event.m_x,event.m_y);
        if(ActionDessin::anyEdgeReached(d)) correctDimension(d);
        d->Refresh () ;
    }
}

void ActionDessinGrab::deplacer(Dessin* d,long entree_mouv_x,long entre_mouv_y){
        long x1,y1,x2,y2;
        x1 = d->GetPosition().x;
        y1 = d->GetPosition().y;
        x2 = x1+(d->GetSize().GetWidth());
        y2 = y1+(d->GetSize().GetHeight());

        
        x1+=(entree_mouv_x-debut_mouv_x);
        y1+=(entre_mouv_y-debut_mouv_y);
        x2+=(entree_mouv_x-debut_mouv_x);
        y2+=(entre_mouv_y-debut_mouv_y);

        d->SetSize(x1,y1,x2-x1,y2-y1);

}

void ActionDessinGrab::correctDimension(Dessin* d){
    long x1,y1,x2,y2;
    x1 = d->GetPosition().x;
    y1 = d->GetPosition().y;
    x2 = x1+((d->GetSize()).GetWidth());
    y2 = y1+((d->GetSize()).GetHeight());

    if(ActionDessin::nEdgeReached(d)){
        y2-=y1;
        y1=0;        
    }
    if(ActionDessin::wEdgeReached(d)){
        x2-=x1;
        x1=0;        
    }
    if(ActionDessin::eEdgeReached(d)){
        x1-=(x2-d->GetParent()->GetSize().GetWidth());
        x2=d->GetParent()->GetSize().GetWidth();
    }
    if(ActionDessin::sEdgeReached(d)){
        y1-=(y2-d->GetParent()->GetSize().GetHeight());
        y2=d->GetParent()->GetSize().GetHeight();
    }
    d->SetSize(x1,y1,x2-x1,y2-y1);
}

void ActionDessinGrab::OnTimer(Dessin* d,wxTimerEvent &event){}

